feat(core): add cloneable resource registry for structured clone#32672
Merged
bartlomieju merged 2 commits intodenoland:mainfrom Mar 12, 2026
Merged
feat(core): add cloneable resource registry for structured clone#32672bartlomieju merged 2 commits intodenoland:mainfrom
bartlomieju merged 2 commits intodenoland:mainfrom
Conversation
Adds infrastructure for custom JS objects to support structured cloning
via `postMessage`/`MessageChannel`. This enables objects like
`CryptoKey` and `X509Certificate` to be cloned across message ports.
The mechanism works by:
1. Objects set `[core.hostObjectBrand]` to a serializer function that
returns `{ type: "<name>", ...data }`
2. Extensions register a deserializer via
`core.registerCloneableResource(name, deserializerFn)`
3. During deserialization, the registry is consulted to reconstruct
the original object
Changes:
- `libs/core/01_core.js`: Add `registerCloneableResource` /
`getCloneableDeserializers` registry, auto-pass deserializers
in `structuredClone`
- `libs/core/ops_builtin_v8.rs`: Mark `op_deserialize` as reentrant
so deserializer callbacks can invoke ops
- `ext/web/13_message_port.js`: Pass cloneable deserializers during
message deserialization
Ref: denoland#12067
Ref: denoland#12734
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2 tasks
Tests cover: - structuredClone with a cloneable object - serialize/deserialize round-trip with cloneable object - cloneable object nested inside a plain object - duplicate registration throws Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
kajukitli
approved these changes
Mar 12, 2026
Contributor
kajukitli
left a comment
There was a problem hiding this comment.
Reviewed the changes. No issues found.
crowlKats
approved these changes
Mar 12, 2026
bartlomieju
added a commit
to bartlomieju/deno
that referenced
this pull request
Mar 12, 2026
Uses the cloneable resource registry from denoland#32672 to enable structured cloning of CryptoKey objects. This allows CryptoKeys (including non-extractable ones) to be passed to Workers via postMessage and cloned with structuredClone(). Closes denoland#12734 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This was referenced Mar 12, 2026
bartlomieju
added a commit
that referenced
this pull request
Mar 13, 2026
## Summary - Enables `structuredClone()` and `postMessage()` for `CryptoKey` objects using the cloneable resource registry from #32672 - Works for all key types (AES, RSA, HMAC, EC, Ed25519, X25519, X448) including **non-extractable** keys - Clones internal key data directly, bypassing the public `exportKey` API Closes #12734 --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
bartlomieju
added a commit
that referenced
this pull request
Mar 13, 2026
## Summary - Enables `structuredClone()` and `postMessage()` for `DOMException` objects using the cloneable resource registry from #32672 - Serializes `message`, `name`, and `stack`; `code` is derived from `name` on deserialization - Supersedes #31156 Ref #31126 (comment) Closes #31983 --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
postMessage/MessageChannel[core.hostObjectBrand]to a serializer function returning{ type: "<name>", ...data }, then register a deserializer viacore.registerCloneableResource(name, fn)op_deserializeas reentrant so deserializer callbacks can invoke opsThis enables objects like
CryptoKey(#12734) andX509Certificateto be cloned across message ports, addressing #12067.For reference, here's how this approach compares to Node.js's implementation of structured cloning for custom objects:
Node.js approach
src/node_messaging.cc—WriteHostObject/ReadHostObject)markTransferMode(this, cloneable, transferable)which sets a private symbol with bitflags (kCloneable = 0x1,kTransferable = 0x2)obj[kClone]()which returns{ data, deserializeInfo: "module:ClassName" }(e.g."internal/crypto/keys:InternalCryptoKey")require()module loading to discover the constructor from thedeserializeInfostring — no explicit registry neededobj[kDeserialize](data)to populate itBaseObjects (via virtual methods) and JS objects (via symbols) in a unified frameworkOur approach
[core.hostObjectBrand]symbol triggerswrite_host_object/read_host_objectinops_builtin_v8.rs(this infrastructure already existed)[core.hostObjectBrand]to a serializer function that returns{ type: "<name>", ...data }core.registerCloneableResource(name, deserializerFn)) mapping type name → deserializer functionRef: #12067
Ref: #12734